package com.by.zhdoctor.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.by.zhdoctor.domain.Click;

import com.by.zhdoctor.repository.ClickRepository;
import com.by.zhdoctor.web.rest.util.HeaderUtil;
import com.by.zhdoctor.web.rest.util.PaginationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Click.
 */
@RestController
@RequestMapping("/api")
public class ClickResource {

    private final Logger log = LoggerFactory.getLogger(ClickResource.class);

    @Inject
    private ClickRepository clickRepository;

    /**
     * POST  /clicks : Create a new click.
     *
     * @param click the click to create
     * @return the ResponseEntity with status 201 (Created) and with body the new click, or with status 400 (Bad Request) if the click has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/clicks")
    @Timed
    public ResponseEntity<Click> createClick(@RequestBody Click click) throws URISyntaxException {
        log.debug("REST request to save Click : {}", click);
        if (click.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("click", "idexists", "A new click cannot already have an ID")).body(null);
        }
        Click result = clickRepository.save(click);
        return ResponseEntity.created(new URI("/api/clicks/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("click", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /clicks : Updates an existing click.
     *
     * @param click the click to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated click,
     * or with status 400 (Bad Request) if the click is not valid,
     * or with status 500 (Internal Server Error) if the click couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/clicks")
    @Timed
    public ResponseEntity<Click> updateClick(@RequestBody Click click) throws URISyntaxException {
        log.debug("REST request to update Click : {}", click);
        if (click.getId() == null) {
            return createClick(click);
        }
        Click result = clickRepository.save(click);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("click", click.getId().toString()))
            .body(result);
    }

    /**
     * GET  /clicks : get all the clicks.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of clicks in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/clicks")
    @Timed
    public ResponseEntity<List<Click>> getAllClicks(Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of Clicks");
        Page<Click> page = clickRepository.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/clicks");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /clicks/:id : get the "id" click.
     *
     * @param id the id of the click to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the click, or with status 404 (Not Found)
     */
    @GetMapping("/clicks/{id}")
    @Timed
    public ResponseEntity<Click> getClick(@PathVariable Long id) {
        log.debug("REST request to get Click : {}", id);
        Click click = clickRepository.findOne(id);
        return Optional.ofNullable(click)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /clicks/:id : delete the "id" click.
     *
     * @param id the id of the click to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/clicks/{id}")
    @Timed
    public ResponseEntity<Void> deleteClick(@PathVariable Long id) {
        log.debug("REST request to delete Click : {}", id);
        clickRepository.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("click", id.toString())).build();
    }


    /**
     * GET  /clicks/:clicker : get the "clicker" clicks.
     *
     * @param clicker the id of the click to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the click, or with status 404 (Not Found)
     */
    @GetMapping("/clicks-clicker/{clicker}")
    @Timed
    public ResponseEntity<List<Click>> getClicksByClicker(@PathVariable String clicker,Pageable pageable) throws URISyntaxException {
        log.debug("REST request to get Click : {}", clicker);
        Page<Click> page= clickRepository.findAllByClicker(clicker,pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/clicks/{clicker}");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }


    /**
     * GET  /clicks/:topic : get the "topic" clicks.
     *
     * @param topic the id of the click to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the click, or with status 404 (Not Found)
     */
    @GetMapping("/clicks-topic/{topic}")
    @Timed
    public ResponseEntity<List<Click>> getClicksByTopic(@PathVariable int topic,Pageable pageable) throws URISyntaxException {
        log.debug("REST request to get Click : {}", topic);
        Page<Click> page= clickRepository.findAllByTopic(topic,pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/clicks/{topic}");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
